package com.baizhi.zjs.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.apache.struts2.ServletActionContext;

import com.baizhi.zjs.dao.BookDAO;
import com.baizhi.zjs.dao.ItemDAO;
import com.baizhi.zjs.entity.BookEntity;
import com.baizhi.zjs.entity.ItemEntity;
import com.baizhi.zjs.entity.OrderEntity;
import com.baizhi.zjs.entity.ShopingCat;
import com.baizhi.zjs.util.MybatisUtil;
import com.baizhi.zjs.util.UUIDUtil;

public class ItemServiceImpl implements ItemService{
	/*id                   VARCHAR2(40)                    not null,
	book_name            VARCHAR2(20),   ==图书名
	book_src             VARCHAR2(20),   ==图片路径
	price                NUMERIC(8, 2),  ==价格
	count                NUMBER(8),       =数量
	amount               NUMERIC(8, 2),   =小计
	book_id              VARCHAR2(40),  =图书id
	order_id             VARCHAR2(40),   =订单id*/
	
//添加购物项
	@Override
	public void addItem(OrderEntity order) {
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		ItemDAO dao = sqlSession.getMapper(ItemDAO.class);
		/*BookService bookimpl = new BookServiceImpl();*/
		BookDAO bookdao = sqlSession.getMapper(BookDAO.class);
		
		//创建list集合
		/*List<ItemEntity> list = new ArrayList<>();*/
		// TODO Auto-generated method stub
		HttpSession session = ServletActionContext.getRequest().getSession();
		
		//获取购物车
		Map<String, ShopingCat> shopCat = (Map<String, ShopingCat>)session.getAttribute("shopcat");
		System.out.println("键值对遍历集合=============");
		Set<Map.Entry<String,ShopingCat>> entSet=shopCat.entrySet();
		System.out.println("购物车的遍历");
		try {
		for (Map.Entry<String, ShopingCat> entry : entSet) {
			if(entry.getValue().getState()==1){
			System.out.println(entry.getKey());
			System.out.println(entry.getValue());
			//判断图书状态是1的添加到购物项
			//获取UUid
			String uuid = UUIDUtil.getUuid();
			//创建订单项对象
			ItemEntity itemEntity = new ItemEntity(uuid,entry.getValue().getBook().getBookname(),entry.getValue().getBook().getSrc(),entry.getValue().getBook().getDdprice(),
					entry.getValue().getCount(),entry.getValue().getDd(),entry.getKey(),order.getId());
			//数据存入数据库
			//BookEntity book = new BookEntity();
			System.out.println("创建book对象");
			System.out.println(entry.getValue().getCount());
			entry.getValue().getBook().setRepertory(entry.getValue().getBook().getRepertory() - entry.getValue().getCount());
			System.out.println("修改的库存");
			entry.getValue().getBook().setSales(entry.getValue().getBook().getSales() + entry.getValue().getCount());
			System.out.println("修改的数量");
			System.out.println(entry.getValue().getBook());
				//将数据存入数据库
				dao.addItem(itemEntity);
				//更改图书的销量和库存
				System.out.println("添加数据库结束");
				System.out.println("修改book======");
				bookdao.updateBook(entry.getValue().getBook());
				System.out.println("ok------");
				//bookimpl.updateOneBook(book);
		}
	}
		MybatisUtil.commit(sqlSession);
		MybatisUtil.close(sqlSession);
		System.out.println("删除购物车");
		session.removeAttribute("shopcat");
		} catch (Exception e) {
			// TODO: handle exception
			MybatisUtil.rollback(sqlSession);
			MybatisUtil.close(sqlSession);
		}
	}
	
}
